<template>
	<view>
		<cu-custom bgColor="bg-red" :isBack="true">		<block slot="backText"></block>
			<block slot="content">个人信息</block>
		</cu-custom>
		<block v-if="member">
			<!-- <view class="text-center margin-top">
				<view class="cu-avatar xl round default-bg">
					<image :src="member.head_portrait" mode="aspectFill" style="z-index: 99999999;"></image>
				</view>
			</view> -->
			
			<!-- <ImageUploadView ref="ImageUploadView" :max="'1'" stitle="更换头像(200*200)"></ImageUploadView> -->
			<!-- :server-url="url"  :value="imageData" :enableDrag=false   :fileKeyName='fileKeyName' -->
			<!-- <robby-image-upload  v-model="head_upload" @add="addImage"   :showUploadProgress="false" :limit='1'  ></robby-image-upload> -->
			<view class="cu-form-group margin-top">
				<view class="title">更换头像(200*200)</view>
			<robby-image-upload  v-model="head_upload" @add="addImage"   :showUploadProgress="false" :limit='1'  ></robby-image-upload>
			</view>
			
			<!-- <view class="cu-form-group margin-top">
				<view class="title">上传我的二维码</view>
			<robby-image-upload  v-model="my_qrcode_upload" @add="addImage"   :showUploadProgress="false" :limit='1'  ></robby-image-upload>
			</view> -->
			<!-- <robby-image-upload  v-model="my_qrcode_upload" @add="addImage"   :showUploadProgress="false" :limit='1'  ></robby-image-upload> -->
			
			<!-- <ImageUploadView ref="ImageUploadView2" :max="'1'" stitle="上传我的二维码"></ImageUploadView> -->
			<!-- <ImageUploadView ref="ImageUploadView3" :max="'1'" stitle="上传微信收款码"></ImageUploadView> -->
			<view class="cu-form-group margin-top">
				<view class="title">昵称</view>
				<input placeholder="请输入昵称" name="input" v-model="member.nickname" class="text-right"></input>
			</view>
			<view class="cu-form-group ">
				<view class="title">姓名</view>
				<input placeholder="请输入姓名" name="input" v-model="member.realname" class="text-right"></input>
			</view>
			<view class="cu-form-group ">
				<view class="title">微信号</view>
				<input placeholder="请输入微信号" name="input" v-model="member.wx_member" class="text-right"></input>
			</view>
			<view class="cu-form-group ">
				<view class="title">性别</view>
				<view>
					<radio-group class="block" @change="RadioChange">
						<radio class="margin-right" :class="member.gender=='0'?'checked':''"
							:checked="member.gender=='0'?true:false" value="0">隐秘</radio>
						<radio class="margin-right" :class="member.gender=='1'?'checked':''"
							:checked="member.gender=='1'?true:false" value="1">男</radio>
						<radio :class="member.gender=='2'?'checked':''" :checked="member.gender=='2'?true:false"
							value="2">女</radio>
					</radio-group>
				</view>
			</view>

			<view class="margin-top-xl margin-lr padding-top-xl">
				<view class="cu-btn  lg round w100 light" :class="'bg-'+theme" v-if="submitLoading">
					<text class="cuIcon-loading2 cuIconfont-spin"></text> 保存中
				</view>
				<view class="cu-btn w100 lg round" :class="'bg-'+theme" v-else @tap="onSubmit">保存</view>
			</view>
		</block>
	</view>
</template>

<script>
	import ImageUploadView from '@/components/default/ImageUploadView.vue';
	import robbyImageUpload from '@/components/robby-image-upload/robby-image-upload.vue';
	export default {
		components: {
			ImageUploadView,
			robbyImageUpload
		},
		
		data() {
			return {
				member: null,
				modalName: '',
				submitLoading: false,
				theme: this.$config.THEME(),
				url:'http://mmx.ycmyw.com/api/v1/file/images?access-token=fi9musW9tPAebcJxMDk_LfVRa4vHNG5Y_1636108429&',
				fileKeyName:'file',
				imageData:[],
				head_portrait:[],
				head_upload:[],
				my_qrcode_upload:[],
				
			}
		},
		onLoad(options) {
			if (this.$common.loginAuth(true)) {
				this.$common.getMember().then((member) => {
					this.member = member
					let _this = this;

				})
			}
		},
		onReady() {
					this.$common.setTitle('个人信息');
				},

		onShow() {

			this.$common.rmCache('member')
			this.$common.getMember().then((member) => {
				this.member = member
				// if (this.member.head_portrait) this.$refs.ImageUploadView.setData([this.member.head_portrait])
				if (this.member.head_portrait) this.head_upload =[this.member.head_portrait]
				if (this.member.my_qrcode) this.my_qrcode_upload =[this.member.my_qrcode]
				// ImageUploadNew
				// if (this.member.my_qrcode) this.$refs.ImageUploadView2.setData([this.member.my_qrcode])
				// if (this.member.wx_qrcode) this.$refs.ImageUploadView3.setData([this.member.wx_qrcode])
				
				// console.log('member',this.member)
			})
			
			// if (this.member.head_portrait) this.$refs.ImageUploadView.setData([this.member.head_portrait])
		},
		methods: {
			addImage: function(e){
			                console.log(e)
			            },
			RadioChange(e) {
				this.member.gender = e.detail.value
			},
			onloa() {
				// console.log(this.)
			},
			onSubmit() {
				if (this.submitLoading) return
				// this.submitLoading = true
				const _this = this;
				let formData = {
					nickname: this.member.nickname,
					realname: this.member.realname,
					gender: this.member.gender,
					wx_member: this.member.wx_member,
				}
				// this.$refs.ImageUploadView.getUploadList().then((res) => {
				// 	this.submitLoading = true
				// 	if (res && res.length > 0) {
				// 		formData.head_portrait = res[0]
				// 		this.osDD(formData)
				// 	} else {
				// 		this.submitLoading = false
				// 		this.$common.showError('头像不能为空')
				// 		return false
				// 	}
				// })
				
				console.log('this.head_upload',this.head_upload)
				if(this.head_upload.length > 0){
					 	this.submitLoading = true
					formData.head_portrait = this.head_upload[0]
					this.osDD(formData)
				}else{
					this.submitLoading = false
					this.$common.showError('头像不能为空')
					return false
				}
			},
			osDD(formData){
				// this.$refs.ImageUploadView2.getUploadList().then((res) => {
				// 	this.submitLoading = true
				// 	if (res && res.length > 0) {
				// 		formData.my_qrcode = res[0]
				// 	}
				// })
				
				// console.log('this.my_qrcode_upload',this.my_qrcode_upload)
				// if(this.my_qrcode_upload.length > 0){
				// 	formData.my_qrcode = this.my_qrcode_upload[0]
				
					// if (!formData.my_qrcode || !formData.my_qrcode.trim()) {
					// 	this.submitLoading = false
					// 	this.$common.showError('我的二维码不能为空')
					// 	return
					// }
					
					if (!formData.nickname.trim()) {
						this.$common.showError('昵称不能为空')
						this.submitLoading = false
						return
					}
					if (!formData.realname.trim()) {
						this.submitLoading = false
						this.$common.showError('姓名不能为空')
						return
					}
					if (!formData.wx_member.trim()) {
						this.submitLoading = false
						this.$common.showError('微信号不能为空')
						return
					}
				
					let _this = this
					this.$http
						.post('member_update', formData)
						.then((response) => {
							this.$common.rmCache('member')
							if (formData.my_qrcode) {
								this.member.my_qrcode = formData.my_qrcode
							}
							if (formData.head_portrait) {
								this.member.head_portrait = formData.head_portrait
							}
							if (formData.my_qrcode) {
								this.member.my_qrcode = formData.my_qrcode
							}
							// this.$common.showSuccess('保存成功')
							this.$common.showSuccess('保存成功', function() {
								uni.navigateTo({
									url: '/pages/member/index'
								})
							})
							this.submitLoading = false
						})
						.catch((response) => {
							this.submitLoading = false
						})
				// }else{
				// 	this.submitLoading = false
				// 	this.$common.showError('我的二维码不能为空')
				// 	return false
				// }
			},
			onSubmit2() {
				if (this.submitLoading) return
				// this.submitLoading = true
				const _this = this;
				let formData = {
					nickname: this.member.nickname,
					realname: this.member.realname,
					gender: this.member.gender,
					wx_member: this.member.wx_member,
				}
				if (!formData.head_portrait.trim()) {
					_this.$common.showError('头像不能为空')
					return
				}
				// if (!formData.my_qrcode.trim()) {
				// 	_this.$common.showError('我的二维码不能为空')
				// 	return
				// }
				if (!formData.nickname.trim()) {
					_this.$common.showError('昵称不能为空')
					return
				}
				if (!formData.realname.trim()) {
					_this.$common.showError('姓名不能为空')
					return
				}
				if (!formData.wx_member.trim()) {
					_this.$common.showError('微信号不能为空')
					return
				}

				this.$refs.ImageUploadView.getUploadList().then((res) => {
					this.submitLoading = true
					if (res && res.length > 0) {
						formData.head_portrait = res[0]
					}
					let _this = this
					this.$http
						.post('member_update', formData)
						.then((response) => {
							this.$common.rmCache('member')
							if (formData.head_portrait) {
								this.member.head_portrait = formData.head_portrait
							}

							// this.$common.showSuccess('保存成功')
							this.$common.showSuccess('保存成功', function() {
								uni.navigateTo({
									url: '/pages/member/index'
								})
							})
							this.submitLoading = false
						})
						.catch((response) => {
							this.submitLoading = false
						})
				})

				this.$refs.ImageUploadView2.getUploadList().then((res) => {
					this.submitLoading = true
					if (res && res.length > 0) {
						formData.my_qrcode = res[0]
					}
					let _this = this
					this.$http
						.post('member_update', formData)
						.then((response) => {
							this.$common.rmCache('member')
							if (formData.my_qrcode) {
								this.member.my_qrcode = formData.my_qrcode
							}
							if (formData.my_qrcode) {
								this.member.my_qrcode = formData.my_qrcode
							}
							// this.$common.showSuccess('保存成功')
							this.$common.showSuccess('保存成功', function() {
								uni.navigateTo({
									url: '/pages/member/index'
								})
							})
							this.submitLoading = false
						})
						.catch((response) => {
							this.submitLoading = false
						})
				})
				this.$refs.ImageUploadView3.getUploadList().then((res) => {
					this.submitLoading = true
					if (res && res.length > 0) {
						formData.wx_qrcode = res[0]
					}

					let _this = this
					this.$http
						.post('member_update', formData)
						.then((response) => {
							this.$common.rmCache('member')
							if (formData.wx_qrcode) {
								this.member.wx_qrcode = formData.wx_qrcode
							}
							if (formData.wx_qrcode) {
								this.member.wx_qrcode = formData.wx_qrcode
							}
							// this.$common.showSuccess('保存成功')
							this.$common.showSuccess('保存成功', function() {
								uni.navigateTo({
									url: '/pages/member/index'
								})
							})
							this.submitLoading = false
						})
						.catch((response) => {
							this.submitLoading = false
						})
				})
			}
		},
	}
</script>

<style>
</style>
